import React, { Component } from 'react'
import {ListBox} from '../../components'
import UpdateProductCombPage from './Update'
import ProductComForm from './productComForm'
import { fetchData } from '../../components/AjaxConsumer'
import { Modal } from 'antd';

export default class ListProductCombPage extends Component {
    constructor(props) {
        super(props);

        this.state = {
            loading: false,
            visible: false,
            addFlag: false,
            formData:{},
            submitUrl:'',
        }

        //行操作列表
        this.rowOperation = [

        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addProductComb',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListProductCombColumnConfig',
            data: [
                {
                    showName: 'groupId',
                    columnName: 'groupId',
                    columnHidden:true,
                    foreverHidden: true
                },{ showName: '组合编码',
                    columnName: 'groupCode',
                    columnWidth: 130
                },{
                    showName: '组合描述',
                    columnName: 'groupName'
                },{
                    showName: '产品编码',
                    columnName: 'productCode'
                },{
                    showName: '产品描述',
                    columnName: 'productName',
                    columnWidth: 200
                },{
                    showName: '创建人',
                    columnName: 'createBy',
                    columnHidden:true
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnHidden:true
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy',
                    columnHidden:true
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified',
                    columnHidden:true
                }
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            {
                showName: '组合编码',
                name:'groupCode',
                type: 'input',
                url: null
            },{
                showName: '组合描述',
                name:'groupName',
                type: 'input',
                url: null
            },{
                showName: '产品编码',
                name:'productCode',
                type: 'input',
                url: null
            },{
                showName: '产品名称',
                name:'productName',
                type: 'input',
                url: null
            },{
                showName: '创建人',
                name:'createBy',
                type: 'input',
                url: null
            },{
                showName: '创建时间',
                name:'gmtCreate',
                type: '',
                url: null
            },{
                showName: '更新人',
                name:'lastModifiedBy',
                type: 'input',
                url: null
            },{
                showName: '更新时间',
                name:'gmtModified',
                type: '',
                url: null
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'addProductComb'){
            this.addProductCombHandle();
        }
    }
    // 增加用户
    addProductCombHandle = () => {
        //this.refs.updateProductComb.addProductCombHandle();

        this.setState({
            formData:{},
            submitUrl:'/base/data/productComb/c'
        })
        this.showModal();

    }
    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    handleCancel = () => {
        this.setState({
            visible: false
        });
    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            //this.refs.updateProductComb.editHandle(rowObj[0].data);


            let id = rowObj[0].data.groupId;

            let options={
                url:'/base/data/productComb/r/'+id,
                type: 'get',
                condition:{}
            };

            fetchData(this, options, (resData) => {
                if (resData.code == '1'){
                    this.setState({
                        formData:resData.data,
                        submitUrl:'/base/data/productComb/u'
                    });
                    this.showModal();
                }else {
                    message.error(resData.msg);
                }
            });


        }

    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateProductComb.deleteHandle(rowObj[0].data)
        }
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listProductComb.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'listProductComb'
                    isHideLeft={isHideLeft}
                    title=" 产品组合"
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/productComb/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />

               {/* <UpdateProductCombPage
                        ref="updateProductComb"
                        refreshHandle = {this.refreshHandle}
                />*/}

                <Modal
                    style={{top: '20px'}}
                    visible={this.state.visible}
                    title="产品组合维护"
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                    width={'850px'}
                    footer=''
                    maskClosable={false}>
                    <ProductComForm
                        onCancel={this.handleCancel}
                        ref="productComForm"
                        submitUrl={this.state.submitUrl}
                        formData={this.state.formData}
                        init={this.state.visible}
                        refreshHandle={this.refreshHandle}
                    />
                </Modal>
            </div>
        )
    }
}
